package hello.app.component;

import hello.app.R;

import java.util.ArrayList;

import android.content.Context;
import android.telephony.PhoneNumberUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class HistoryAdapter extends ArrayAdapter<HistoryItem> {

	public HistoryAdapter(Context context, int textViewResourceId, ArrayList<HistoryItem> items) {
		super(context, textViewResourceId, items);

	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// ビューを受け取る
		View view = convertView;
		if (view == null) {
			LayoutInflater inflater = (LayoutInflater) getContext()
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = inflater.inflate(R.layout.history_row, null);
		}

		HistoryItem item = getItem(position);
		if (item != null) {
			ImageView image = (ImageView)view.findViewById(R.id.profileImage);
			ImageView typeImage = (ImageView)view.findViewById(R.id.type_image);
			TextView number = (TextView)view.findViewById(R.id.row_text_number);
			TextView datetime = (TextView)view.findViewById(R.id.row_text_datetime);
			TextView eval = (TextView)view.findViewById(R.id.row_text_eval);
			TextView name = (TextView)view.findViewById(R.id.row_text_name);
			TextView others = (TextView)view.findViewById(R.id.row_text_others);
			number.setText(PhoneNumberUtils.formatNumber(item.getNumber()));
			datetime.setText(item.getDatetime());
			eval.setText(item.getEval());
			name.setText(item.getName());
			String str = item.getOthers();
			others.setText(str);

			if(str.length() == 0)
				others.setVisibility(View.GONE);
			else
				others.setVisibility(View.VISIBLE);

			if(!item.isPersonal()){
				image.setImageResource(R.drawable.community);
			}else{
				image.setImageResource(R.drawable.personal);
			}
			switch(item.getType()){
			case HistoryItem.INCOMING:
				typeImage.setImageResource(R.drawable.incoming);
				break;
			case HistoryItem.OUTGOING:
				typeImage.setImageResource(R.drawable.outgoing);
				break;
			case HistoryItem.MISSED:
				typeImage.setImageResource(R.drawable.missed);
				break;
			}
		}
		return view;
	}
}
